﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Prism.Events;
using System.Collections.ObjectModel;
using System.ComponentModel;
using ModuleExternalProducers.Views;
using ServerConnection;
using ServerConnection.ShopServiceReference;
using EventInfrastructure;
using Microsoft.Practices.ServiceLocation;
using ShellInfrastructure;

namespace ModuleExternalProducers.ViewModels
{
    public class ExternalProducersViewModel : IProducersViewModel, INotifyPropertyChanged
    {
        #region Private members

        private IEventAggregator m_eventAggregator;

        private ObservableCollection<ExternalProducerView> externalProducersCollection;

        private ObservableCollection<CategoryView> categoriesOfProducer;

        private ExternalProducerView m_selectedProducer;

        [Dependency]
        public IRegionManager RegionManager { get; set; }

        /// <summary>
        /// This method is called up when the property is changed
        /// </summary>
        /// <param name="propertyName"></param>
        private void m_OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                //fire event
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        /// <summary>
        /// Refreshlist of producers after adding a new one
        /// </summary>
        /// <param name="id"></param>
        private void m_onUpdateProducerList(int id)
        {
            RegionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
            IRegion region = RegionManager.Regions[RegionNames.LeftRegionName];
            region.Activate(ServiceLocator.Current.GetInstance<IProducersView>());           

            if (Service.Instance.GetProducers() != null)
            {
                List<ExternalProducerView> tmpPlaceView = Service.Instance.GetProducers().ToList();


                externalProducersCollection = new ObservableCollection<ExternalProducerView>(tmpPlaceView);

                LoadExternalProducers = externalProducersCollection;
                //m_eventAggregator.GetEvent<AddOrUpdatePlaceEvent>().Subscribe(m_onUpdatePlacesList);
            }     
        }

        

        #endregion Private members

        
        #region Constructor

        public ExternalProducersViewModel(IEventAggregator eventAggregator)
        {
            this.m_eventAggregator = eventAggregator;

            m_eventAggregator.GetEvent<ProducerAddEvent>().Subscribe(m_onUpdateProducerList);

            //LoadPlaces = Service.Instance.GetPlaces("eng").ToList()ж as ObservableCollection<PlaceView>;
            if (Service.Instance.GetProducers() != null)
            {
                List<ExternalProducerView> tmpPlaceView = Service.Instance.GetProducers().ToList();


                externalProducersCollection = new ObservableCollection<ExternalProducerView>(tmpPlaceView);

                LoadExternalProducers = externalProducersCollection;
                //m_eventAggregator.GetEvent<AddOrUpdatePlaceEvent>().Subscribe(m_onUpdatePlacesList);
            }
        }

        #endregion Constructor

        #region Public properties

        public ObservableCollection<ExternalProducerView> LoadExternalProducers
        {
            get
            {
                return externalProducersCollection;
            }
            set
            {
                externalProducersCollection = value;
                m_OnPropertyChanged("LoadExternalProducers");
            }
        }
          
        /// <summary>
        /// Property changes, when user selects concrete producer and then get categories
        /// </summary>
        public ObservableCollection<CategoryView> LoadCategory 
        {
            get
            {
                return categoriesOfProducer;
            }
            set
            {
                categoriesOfProducer = value;
                m_OnPropertyChanged("LoadCategory");
            }
        }

        /// <summary>
        /// Used when producer is selected in ListBox
        /// </summary>
        public ExternalProducerView ExternalProducerView
        {
            get
            {
                return m_selectedProducer;
            }
            set
            {
                m_selectedProducer = value;

                if (m_selectedProducer != null)
                {
                    m_eventAggregator.GetEvent<ProducerSelectEvent>().Publish(m_selectedProducer.Id);                    
                }
            }
        }


        #endregion Public properties

        public void DoLoadCategories(int id)
        {
            List<CategoryView> cats = Service.Instance.GetCategoriesOfProducer("eng", id, 0).ToList(); ;

            ObservableCollection<CategoryView> catsObserv = new ObservableCollection<CategoryView>(cats);

            LoadCategory = catsObserv;
        }

        #region Events

        /// <summary>
        /// PropertyChanged is needed for binding to ui
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion Events

    }//class
}
